﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CuaHangDiaNhac
{
    public partial class frmChiTietHoaDon : DevComponents.DotNetBar.Office2007Form
    {
        StoreCDDataContext dt = new StoreCDDataContext();
        public frmChiTietHoaDon(int maso)
        {
            InitializeComponent();
             var hd = dt.HoaDonBan_SelectByID(maso).First() ;
           
            if (hd != null)
            {
                txtMHD.Text = hd.MaHDBH.ToString();
                txtNDHD.Text = hd.NoiDungHD;
                txtSDT.Text = hd.SoDT.ToString();
                txtTenkh.Text = hd.TenKH;
                List<ChiTietHoaDonBanHang> ct =(from ctiet in dt.ChiTietHoaDonBanHangs
                 where ctiet.MaHDBH == maso
                 select ctiet).ToList<ChiTietHoaDonBanHang>();
                
                dataGridViewX1.DataSource = ct;
            }
        }

        private void frmChiTietHoaDon_Load(object sender, EventArgs e)
        {
            getdata();
        }
        void getdata()
        {
            dgrDanhSach.DataSource = (from dn in dt.DiaNhacs
                                      select new { dn.MaDia, dn.TenDia, dn.DonGiaBan });
            
        }
        bool TonTai(int s)
        {
            return (from d in dt.HoaDonBans
                    where d.MaHDBH == s
                    select d).Count() > 0;
        }
        private void btnThem1_Click(object sender, EventArgs e)
        {
            if (TonTai(int.Parse(txtMHD.Text)))
                lblThongBao.Text = "Tồn tại mã hóa đơn";
            else
            {
                HoaDonBan hd = new HoaDonBan();
                hd.MaHDBH = ConvertType.ToInt(txtMHD.Text);
                hd.NoiDungHD = txtNDHD.Text;
                hd.NgayLapHD = dttNgay.Value;
                hd.TenKH = txtTenkh.Text;
                hd.SoDT = ConvertType.ToInt(txtSDT.Text);
                dt.HoaDonBans.InsertOnSubmit(hd);
                dt.SubmitChanges();
                getdata();
            }

            // hd.TongTien

        }
        decimal tongtien = 0;
        private void btnThem_Click(object sender, EventArgs e)
        {
            DataGridViewRow r = dgrDanhSach.CurrentRow;
            decimal a = ConvertType.ToInt(txtSoLuong.Text);
            decimal thanhtien = a * ConvertType.ToInt(r.Cells["DonGia"].Value);
            tongtien += thanhtien;
            object[] row = { r.Cells[0].Value, r.Cells[1].Value, txtSoLuong.Text, thanhtien };
            //dgrDanhSach.Rows.Remove(r);
            dataGridViewX1.Rows.Add(row);
            lblTongTien.Text = "Thành Tiền :" + tongtien.ToString() + " VNĐ";
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow r = dataGridViewX1.CurrentRow;
                DataGridViewRow r1 = new DataGridViewRow();
                decimal thanhtien = ConvertType.ToDecimal(r.Cells["ThanhTien"].Value);
                tongtien -= thanhtien;
                //r1.Cells[0].Value = r.Cells[0].Value;
                //r1.Cells[1].Value = r.Cells[1].Value;
                //dgrDanhSach.Rows.Add(r1);
                dataGridViewX1.Rows.Remove(r);
                lblTongTien.Text = "Thành Tiền :" + tongtien.ToString() + " VNĐ";
            }
            catch (Exception)
            {


            }

        }

        private void buttonX2_Click(object sender, EventArgs e)
        {

        }

        private void btnDSDia_Click(object sender, EventArgs e)
        {

        }


    }
}
